package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.dto.OrderDTO;
import com.itheima.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/findById")
    public Result findById(Integer id) {
        try {
            Order order = orderService.findById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @PostMapping("/submitOrder")
    public Result submitOrder(@RequestBody OrderDTO orderDTO) {
        try {
            orderService.submitOrder(orderDTO);
            return new Result(true, MessageConstant.ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().equals(MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL)){
                return new Result(false, MessageConstant.TELEPHONE_VALIDATECODE_NOTNULL);
            }else if (e.getMessage().equals(MessageConstant.VALIDATECODE_ERROR)){
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }else if (e.getMessage().equals(MessageConstant.ORDER_FULL)){
                return new Result(false, MessageConstant.ORDER_FULL);
            }else if (e.getMessage().equals(MessageConstant.SELECTED_DATE_CANNOT_ORDER)){
                return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
            }else if (e.getMessage().equals(MessageConstant.ORDER_FULL)){
                return new Result(false, MessageConstant.ORDER_FULL);
            }else if (e.getMessage().equals(MessageConstant.HAS_ORDERED)){
                return new Result(false, MessageConstant.HAS_ORDERED);
            }else {
                return new Result(false, MessageConstant.ORDER_FAIL);
            }


        }
    }

}
